package com.fairytask.net
{
	import com.fairytask.data.Modulator;
	import com.fairytask.data.View;
	import com.fairytask.define.ViewDefine;
	import com.fairytask.engine.utils.IO;
	import com.smartfoxserver.v2.SmartFox;
	import com.smartfoxserver.v2.core.SFSEvent;
	import com.smartfoxserver.v2.entities.Room;
	import com.smartfoxserver.v2.entities.data.ISFSObject;
	import com.smartfoxserver.v2.requests.ExtensionRequest;
	import com.smartfoxserver.v2.requests.IRequest;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	public class NetSocket extends EventDispatcher
	{
		private static var _instance	:NetSocket;

		private var model:Modulator;
		public var sfx:SmartFox;
		//是否连接上
		public var connected	:Boolean	=	false;
		
		public static function get instance():NetSocket
		{
			if(null==_instance)
			{
				_instance	=	new NetSocket();
			}
			return _instance;
		}

		public function NetSocket()
		{
			sfx	=	new SmartFox(true);
			model	=	Modulator.instance;
			initEvent();
		}
		
		private function initEvent():void
		{
			/*sfx.addEventListener(SFSEvent.LOGIN, onconnectHandler)
			sfx.addEventListener(SFSEvent.LOGIN_ERROR, onconnectHandler)*/
			sfx.addEventListener(SFSEvent.CONNECTION, onConnection)
			sfx.addEventListener(SFSEvent.CONNECTION_LOST, onConnectionLost)
			sfx.addEventListener(SFSEvent.CONFIG_LOAD_SUCCESS, onConfigLoadSuccess)
			//sfx.addEventListener(SFSEvent.CONFIG_LOAD_FAILURE, onConfigLoadFailure)
				//自定义事件
			sfx.addEventListener(SFSEvent.EXTENSION_RESPONSE, onExtensionResponse);
			//room相关
			sfx.addEventListener(SFSEvent.ROOM_ADD,onRoomResult);
		}
		
		/**
		 * 处理房间信息 
		 * @param event
		 */
		protected function onRoomResult(event:SFSEvent):void
		{
			 var roomView	:View	=	model.scene.GetView(ViewDefine.ROOM);
		}
		
		/**
		 * 扩展调用 
		 * @param event
		 * 
		 */
		protected function onExtensionResponse(event:SFSEvent):void
		{
			model.dispatchEvent(new CustomToClientEvent(event.params.cmd,event.params.params));
		}
		
		protected function onConfigLoadSuccess(event:SFSEvent):void
		{
			trace("加载完毕");
		}
		
		/**
		 * 链接丢失 
		 * @param event
		 * 
		 */
		protected function onConnectionLost(event:SFSEvent):void
		{
			connected	=	false;
			IO.traceLog(" 链接失效了");
		}
		
		/**
		 * 链接上 
		 * @param event
		 */
		protected function onConnection(event:SFSEvent):void
		{
			connected	=	true;
			IO.traceLog("链接成功");
			model.dispatchEvent(event);
		}
		
		/**
		 * 当链接成功 
		 * @param event
		 * 
		 */
		protected function onconnectHandler(event:Event):void
		{
			IO.traceLog("这是怎么了");
		}
		
		public function connect():void
		{
			sfx.loadConfig("config/serverConfig.xml");
		}
		
		/**
		 * 发送请求 
		 * @param pRequest
		 * 
		 */
		public static function send(pRequest:IRequest):void
		{
			instance.sfx.send(pRequest);
		}
		
		/**
		 * 发送自定义请求 
		 * @param pCmd
		 * @param pData
		 * @param pRoom
		 * 
		 */
		public static function sendCustom(pCmd:String,pData:ISFSObject,pRoom:Room=null):void
		{
			instance.sfx.send(new ExtensionRequest(pCmd,pData,pRoom));
		}
	}
}